package com.baylor.broadcast;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.baylor.services.PushMsgService;

/**
 * Created by ketian on 2014/9/25.
 */
public class CheackBroadcastReceiver extends BroadcastReceiver {

    private Context mContext;

    public CheackBroadcastReceiver(Context context) {
        super();
        mContext = context;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("broadcast", "广播检测");
        if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) {
            // Service状态
            boolean isServiceRunning = false;
            ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
            for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {

                Log.d("broadcast", "---"+service.service.getClassName());
                // 检查Service的类名
                if ("com.baylor.services.PushMsgService".equals(service.service.getClassName())) {
                    isServiceRunning = true;
                }
            }
            if (!isServiceRunning) {
                Log.d("broadcast", "检测到服务未开启，并开启服务");
                Intent in = new Intent(context, PushMsgService.class);
                context.startService(in);
            }else{
                Log.d("broadcast", "检测到服务已开启");
            }
        }
    }

}
